package com;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FileCopy extends JFrame implements ActionListener {

	/**
	 * 文件拷贝
	 */
	private static final long serialVersionUID = 4356L;
	private JTextField[] tf = new JTextField[2];
	private JButton[] but = new JButton[2];
	private JButton but1;
	private JProgressBar pb;
	private Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
	private Container container;
	File f;
	File fs;

	public FileCopy() {
		setTitle("文件拷贝");
		setSize(500, 350);
		Dimension framesize = this.getSize();
		int x = (int) screensize.getWidth() / 4 * 3 - (int) framesize.getWidth() / 4;
		int y = (int) screensize.getHeight() / 3 - (int) framesize.getHeight() / 2 * 3 / 2;
		setLocation(x, y);
		setLayout(null);

		Font font = new Font("宋体", Font.TRUETYPE_FONT, 15);
		Font font1 = new Font("宋体", Font.TRUETYPE_FONT, 12);

		String[] str = { "源文件：", "复制到：" };
		for (int i = 0; i < str.length; i++) {
			JLabel label = new JLabel(str[i]);
			label.setBounds(40, 50 + i * 40, 100, 30);
			label.setFont(font);
			add(label);

			tf[i] = new JTextField(20);
			tf[i].setFont(font);
			tf[i].setBounds(120, 50 + i * 40, 240, 30);
			tf[i].addActionListener(this);
			add(tf[i]);

			but[i] = new JButton("···");
			but[i].setFont(font1);
			but[i].setBounds(365, 50 + i * 40, 70, 30);
			but[i].addActionListener(this);
			add(but[i]);

		}

		but1 = new JButton("复  制");
		but1.setFont(font1);
		but1.setBounds(200, 250, 80, 30);
		but1.addActionListener(this);
		add(but1);

		pb = new JProgressBar();
		pb.setBounds(80, 180, 330, 20);
		pb.setStringPainted(true);
		add(pb);
		setVisible(true);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				new FileCopy();
			}
		});
		;
	}

	public void actionPerformed(ActionEvent e) {
		Object ob = e.getSource();
		if (ob == but[0]) {
			JFileChooser fc = new JFileChooser();
			int value = fc.showOpenDialog(fc);
			if (value == JFileChooser.APPROVE_OPTION)
				tf[0].setText(fc.getSelectedFile().getPath());
		}

		if (ob == but[1]) {
			JFileChooser fc = new JFileChooser();
			int value = fc.showOpenDialog(fc);
			if (value == JFileChooser.APPROVE_OPTION)
				tf[1].setText(fc.getSelectedFile().getPath());
		}

		if (ob == but1) {
			FileThreadCopy ftc=new FileThreadCopy(tf[0], tf[1], pb,container);
			Thread th1=new Thread(ftc);
			th1.start();
		}
	}

	/*class Dialog {
		public Dialog(Container container,String msg, String title) {
			JOptionPane.showMessageDialog(container, msg, title, JOptionPane.INFORMATION_MESSAGE);
		}
	}*/

	/*@SuppressWarnings("resource")
	public void readFile() {
		f = new File(tf[0].getText());
		fs = new File(tf[1].getText());
		final double filenum = f.length();

		new Thread(new Runnable() {
			public void run() {
				FileInputStream fis;
				FileOutputStream fos;
				try {
					fis = new FileInputStream(f);
				} catch (FileNotFoundException e) {
					new Dialog("未找到复制文件！", "复制失败");
					return;
				}

				try {
					fos = new FileOutputStream(fs);
				} catch (FileNotFoundException e) {
					new Dialog("未找到复制的目的文件！", "复制失败");
					return;
				}

				byte[] buf = new byte[1024 * 8];
				int sum = 0;
				int b = 0;
                DecimalFormat df=new DecimalFormat("#00");
				
				while (b != -1) {
					try {
						b = fis.read(buf);
						System.out.println("读取：" + Thread.currentThread().getName() + ":  " + b);
					} catch (IOException e) {
						new Dialog("读取文件时错误！", "复制失败");
						return;
					}
                      
					sum += b;
				    String s=df.format(sum/filenum*100);
					pb.setValue(Integer.valueOf(s));
				}
				try {
					fos.write(buf);
				} catch (IOException e) {
					new Dialog("写入文件过程中错误！", "复制失败");
					return;
				}
				System.out.println("写入：" + Thread.currentThread().getName() + ":  " + b);
				try {
					fos.flush();
				} catch (IOException e) {
					new Dialog("写入文件刷新错误！", "复制失败");
					return;
				}
			}
		}).start();
	}*/
}
